clear
clear mata
clear matrix

cd ..																 /* Goes back to parent folder */

local path : pwd

cd "`path'"


capture log close
log using "log\15_Patriotism.log", replace

mkdir tmp/                                                                      /* creates a temporary folder to store temp files */


*       Patriotism!

*       FIRST VERSION  June        9, 2022
*       THIS VERSION   June       15, 2022
*       LAST RUN       June       15, 2022

*       LAST REVISOR		BC

*       Log of revisions:

*       This prepares final data: patriotism, patriotism-panel, patriotism-ASN

********************************************************************************
****                      PLAN OF THE PROCEDURE                             ****
****                                                                        ****
****  1. patriotism			                       				    	    ****
****  	a. Dependent variables			                       			    ****
****  	b. New Deal						                       			    ****
****  	c. WWI controls						                       			****
****  	d. Droughts						                       			    ****
****  	e. Committees						                       			****
****  	f. Politics  						                       			****
****  	g. Censuses  						                       			****
****  		  i. 1910  						                       			****
****  		 ii. 1930 						                       			****
****  		iii. 1940						                       			****
****  	h. Corrections               	                       			    ****
****  		  i. Correct Kenedy, TX                                         ****
****  		 ii. Correct Daggett, UT                                        ****
****  	i. Sample indicators         	                       			    ****
****  	j. Keep what is needed and label variables           			    ****
****  	k. Save patriotism.dta       	                       			    ****
****  2. patriotism-panel		                       				    	****
****  	a. First period		                       			     	    	****
****  	b. Second period		                       			     	    ****
****  	c. Append the two periods                     			     	    ****
****  	d. Keep what is needed and label variables           			    ****
****  	e. Save patriotism.dta       	                       			    ****
****  3. patriotism-ASN		                       				        	****
****  	a. County-level variables		                       				****
****  	b. Create county-level variables			                       	****
****  	c. Save county-level variables						                ****
****  	d. Create individual dataset		                       		    ****
****  	e. Create CEM for farmers and farm-hands                            ****
****  		  i. Prepare database by keeping only variables neeeded         ****
****  		 ii. Create CEM indicators separately for each county           ****
****                 * Loop over every county in the database               ****
****                 * Create CEM indicators                                ****
****                    - Treatment: being a farmer                         ****
****                    - Treatment: being a farm-hand                      ****
****                 * Save CEM indicators for this county and drop it      ****
****  		iii. Append CEM indicators for every county and save            ****
****  	f. Merge everything, keep only what is needed and label variables   ****
****  	g. Save patriotism-asn.dta       	                       			****
****  4. Erase junk				                                            ****
********************************************************************************

********************************************************************************
****  1. patriotism			                       				    	    ****
********************************************************************************
use "data/CountiesND_WGS84", clear

ren _ID _ID_WGS84
merge 1:1 stateicpsr countynd using "data/CountiesND_CA"  , nogen keepus(_ID)
ren _ID _ID_CA
order state-_ID_WGS84 _ID_CA

merge m:1 state               using "rawdata/NARA/ServiceCommands.dta"          	  	  , nogen keep(    mat)    /* Not matched are HI & AK from using */
merge 1:1 stateicpsr countynd using "data/war_bonds_contracts.dta"          		  	  , nogen keep(    mat)    /* Perfect match */
merge 1:1 stateicpsr countynd using "data/volunteers_ww2.dta "              			  , nogen keep(mas mat)    /* Not matched Cochise (AZ) & Yellowstone (MT) */
merge 1:1 stateicpsr countynd using "data/volunteers_ww2_women.dta"         			  , nogen keep(mas mat)    /* No perfect match */
merge 1:1 stateicpsr countynd using "rawdata/Medals/medals.dta"             			  , nogen keep(    mat)    /* Perfect match */
merge 1:1 stateicpsr countynd using "data/new_deal.dta "                    			  , nogen keep(mas mat)    /* Not matched DC & Yellowstone (MT) */
merge 1:1 stateicpsr countynd using "rawdata/WWI_volunteers/volunteers_ww1.dta"           , nogen keep(mas mat)    /* No perfect match */
merge 1:1 stateicpsr countynd using "data/unemplC37.dta"                    			  , nogen keep(    mat)    /* Perfect match */
merge 1:1 stateicpsr countynd using "data/droughts.dta"    				  			  	  , nogen keep(    mat)    /* Perfect match */
merge 1:1 stateicpsr countynd using "data/committees.dta"    				  			  , nogen keep(mas mat)    /* Not matched DC */
merge 1:1 stateicpsr countynd using "data/FDR40.dta"               		  			  	  , nogen keep(mas mat)    /* Not matched 29 counties from master */
merge 1:1 stateicpsr countynd using "data/C10.dta "                         			  , nogen keep(mas mat)    /* Not matched Greenleed (AZ) */
merge 1:1 stateicpsr countynd using "data/C30.dta"                          			  , nogen keep(mas mat)    /* Not matched Yellowstone (MT) */
merge 1:1 stateicpsr countynd using "data/C30BENSS.dta"                          		  , nogen keep(mas mat)    /* Not matched Yellowstone (MT) */
merge 1:1 stateicpsr countynd using "data/C40.dta"                          			  , nogen keep(    mat)    /* Perfect match */
merge 1:1 stateicpsr countynd using "data/agriC30.dta"                       			  , nogen keep(    mat)    /* Perfect match */
merge 1:1 stateicpsr countynd using "data/Dust_Bowl.dta"                   			      , nogen keep(	   mat)    /* Perfect match */
merge 1:1 stateicpsr countynd using "data/frontier.dta"                     			  , nogen keep(    mat)    /* Perfect match */
merge 1:1 stateicpsr countynd using "rawdata/maps/Borders.dta"                     	  	  , nogen keep(    mat)    /* Perfect match */


********************************************************************************
****  	a. Dependent variables			                       			    ****
********************************************************************************
 replace warbond_PC = 0 if stateicpsr == 37 & countynd == 10
								/* Armstrong, ND buys 0 warbonds. However a very small portion of its county overlaps with a county that buys many warbonds. Recoded manually to 0 */
 gen     iwarbond_1944_PC   = asinh(warbond_PC)
 gen     iwarconpro_PC      = asinh(warconpro_PC)
 gen     ww2_vol_pop40      =      (WW2VOLDA / C40POP    )  * 100
 gen     ww2_volw_pop40     =      (WW2WVOLDA / C40POP    ) * 100
 recode  ww2_volw_pop40  (. = 0) 												if servicecommand != 7
 gen     ww2_awards_pop40 = (WW2AWARDS/ C40POP)*1000
 gen 	 ww2_awards_pop40_is = asinh(ww2_awards_pop40)
 pca 	 iwarbond_1944_PC ww2_vol_pop40 ww2_awards_pop40_is 					if servicecommand != 7
 predict pc1                                                                    if servicecommand != 7

********************************************************************************
****  	b. New Deal						                       			    ****
********************************************************************************
 gen     NDEXP_PC 		= NDEXP / C30POP
 gen     AGRI_PF        = (AAA + FCA) / A30FARMERS 
 gen     SSA_PB     	= (SSA_OA + SSA_ADC) / (C30BENSSDC + C30BENSSOAs)
 gen 	 HOLC_PH		= HOLC / C30HOMEOWN
 gen     infra_AL       = (PWAF + PBA + PRA) / (emergtot + unemptot + punempto)
 gen     RFC_PC         = RFC / C30POP
 gen 	 EW_AL        	= emergtot / (emergtot + unemptot + punempto)
 
 for VAR in var NDEXP_PC AGRI_PF SSA_PB HOLC_PH infra_AL RFC_PC: gen iVAR = asinh(VAR)
 
********************************************************************************
****  	c. WWI controls						                       			****
********************************************************************************
 gen     ww1_vol_sh      = WW1VOL       / (WW1VOL + WW1IND)
 gen     ww1_vol_pop10   = (WW1VOL / C10POP) * 100
 gen ww1_awards_pop10    = ((WW1AWARDS) / C10POP)*1000
 gen ww1_awards_pop10_is = asinh(ww1_awards_pop10)
 
********************************************************************************
****  	d. Droughts						                       			    ****
********************************************************************************
 gen SUM3MO_DROUGHT9700 = SUM3MO_DROUGHT1897 + SUM3MO_DROUGHT1898 + SUM3MO_DROUGHT1899 + SUM3MO_DROUGHT1900
 gen SUM3MO_DROUGHT0104 = SUM3MO_DROUGHT1901 + SUM3MO_DROUGHT1902 + SUM3MO_DROUGHT1903 + SUM3MO_DROUGHT1904
 gen SUM3MO_DROUGHT0508 = SUM3MO_DROUGHT1905 + SUM3MO_DROUGHT1906 + SUM3MO_DROUGHT1907 + SUM3MO_DROUGHT1908
 gen SUM3MO_DROUGHT0912 = SUM3MO_DROUGHT1909 + SUM3MO_DROUGHT1910 + SUM3MO_DROUGHT1911 + SUM3MO_DROUGHT1912
 gen SUM3MO_DROUGHT1316 = SUM3MO_DROUGHT1913 + SUM3MO_DROUGHT1914 + SUM3MO_DROUGHT1915 + SUM3MO_DROUGHT1916
 gen SUM3MO_DROUGHT1720 = SUM3MO_DROUGHT1917 + SUM3MO_DROUGHT1918 + SUM3MO_DROUGHT1919 + SUM3MO_DROUGHT1920
 gen SUM3MO_DROUGHT2124 = SUM3MO_DROUGHT1921 + SUM3MO_DROUGHT1922 + SUM3MO_DROUGHT1923 + SUM3MO_DROUGHT1924
 gen SUM3MO_DROUGHT2528 = SUM3MO_DROUGHT1925 + SUM3MO_DROUGHT1926 + SUM3MO_DROUGHT1927 + SUM3MO_DROUGHT1928
 gen SUM3MO_DROUGHT2932 = SUM3MO_DROUGHT1929 + SUM3MO_DROUGHT1930 + SUM3MO_DROUGHT1931 + SUM3MO_DROUGHT1932
 gen SUM3MO_DROUGHT9732 = SUM3MO_DROUGHT1897 + SUM3MO_DROUGHT1898 + SUM3MO_DROUGHT1899 + SUM3MO_DROUGHT1900 + SUM3MO_DROUGHT1901 + SUM3MO_DROUGHT1902 + SUM3MO_DROUGHT1903 + SUM3MO_DROUGHT1904 + SUM3MO_DROUGHT1905 + SUM3MO_DROUGHT1906 + SUM3MO_DROUGHT1907 + SUM3MO_DROUGHT1908 + SUM3MO_DROUGHT1909 + SUM3MO_DROUGHT1910 + SUM3MO_DROUGHT1911 + SUM3MO_DROUGHT1912 + SUM3MO_DROUGHT1913 + SUM3MO_DROUGHT1914 + SUM3MO_DROUGHT1915 + SUM3MO_DROUGHT1916 + SUM3MO_DROUGHT1917 + SUM3MO_DROUGHT1918 + SUM3MO_DROUGHT1919 + SUM3MO_DROUGHT1920 + SUM3MO_DROUGHT1921 + SUM3MO_DROUGHT1922 + SUM3MO_DROUGHT1923 + SUM3MO_DROUGHT1924 + SUM3MO_DROUGHT1925 + SUM3MO_DROUGHT1926 + SUM3MO_DROUGHT1927 + SUM3MO_DROUGHT1928 + SUM3MO_DROUGHT1929 + SUM3MO_DROUGHT1930 + SUM3MO_DROUGHT1931 + SUM3MO_DROUGHT1932
 gen SUM3MO_DROUGHT3340 = SUM3MO_DROUGHT1933 + SUM3MO_DROUGHT1934 + SUM3MO_DROUGHT1935 + SUM3MO_DROUGHT1936 + SUM3MO_DROUGHT1937 + SUM3MO_DROUGHT1938 + SUM3MO_DROUGHT1939 + SUM3MO_DROUGHT1940

 for VAR in var SUM3MO_DROUGHT9700 SUM3MO_DROUGHT0104 SUM3MO_DROUGHT0508 SUM3MO_DROUGHT0912 SUM3MO_DROUGHT1316 						///
				SUM3MO_DROUGHT1720 SUM3MO_DROUGHT2124 SUM3MO_DROUGHT2528 SUM3MO_DROUGHT2932 SUM3MO_DROUGHT3340 SUM3MO_DROUGHT9732 : gen iVAR = asinh(VAR)

********************************************************************************
****  	e. Committees						                       			****
********************************************************************************
 gen iAGRI_T73          = asinh(AGRICULTURE_T73)
 gen DAGRI_T73          = AGRICULTURE_T73 > 0 if AGRICULTURE_T73 != .
 gen iAGRI_T62          = asinh(AGRICULTURE_T62)
 
********************************************************************************
****  	f. Politics  						                       			****
********************************************************************************
 xtile fdr40_10 		= FDR40    							 if pc1 != ., nq(10)

********************************************************************************
****  	g. Censuses  						                       			****
****  		  i. 1910  						                       			****
********************************************************************************
gen lpop10			   = log(C10POP + 1)

********************************************************************************
****  		 ii. 1930 						                       			****
********************************************************************************
gen    lpop30 		   = log(C30POP)
gen    invpop    	   = 1 / C30POP
winsor invpop ,        gen(invpop_w99) p(0.01)
gen    shown_1930      = (A30AFARMERSO + A30AFARMERSP) / A30FARMERS
gen    lowsharec       = shown_1930 >= .5297872 if shown_1930 != .
gen    Yf_T29          = totfarminc29 / A30AFARMS
gen   iYf_T29          = asinh(Yf_T29)
gen   iCATTLE_PF30     = asinh(cattlen30 / A30FARMERS) 

********************************************************************************
****  		iii. 1940						                       			****
********************************************************************************
gen    dlpop           = log(C40POP) - log(C30POP)
xtile  dlpop_pct       = dlpop, n(100)
gen    lowdpop         = dlpop_pct > 5 & dlpop_pct <= 95
gen    lc40wage        = log(c40wage)
xtile  c40w_10         = c40wage  							 if pc1 != ., nq(10)
gen    c40emp 		   = 1 - c40unemp
xtile  c40e_10  = c40emp 									 if pc1 != ., nq(10)

********************************************************************************
****  	h. Corrections               	                       			    ****
****  		  i. Correct Kenedy, TX                                         ****
********************************************************************************
replace iHOLC_PH = asinh(0) if state == "Texas" & countyname == "Kenedy"   		/* Kenedy, TX: receives 0 HOLC, and has only renters => gets 0 HOLC / homeowners */

********************************************************************************
****  		 ii. Correct Daggett, UT                                        ****
********************************************************************************
replace iSSA_PB = asinh(0) if state == "Utah" & countyname == "Daggett"			/*Daggett, UT: has 0 potential beneficiaries from SS.*/

********************************************************************************
****  	i. Sample indicators         	                       			    ****
********************************************************************************
gen sample_ols    =     1                       if iwarbond_1944_PC != . & ww2_vol_pop40 != . & iNDEXP_PC != . & iAGRI_PF != . & iSUM3MO_DROUGHT3340 != . & ww1_vol_sh != . & lpop30  != . & c30unemp != . & c40unemp != . & c30urban1 != . & MEAN9628 != . & c30men != . & c30black != . & c30jap != . & c30deu != . & c30ita != . & c30vet != . & lc40wage != . & iwarconpro_PC != .

egen median_catt1  = median(iCATTLE_PF30                     ) if                    sample_ols==1
gen median_below1  =   cond(iCATTLE_PF30 <  median_catt1, 1,0) if iCATTLE_PF30 !=. & sample_ols==1
gen median_above1  =   cond(iCATTLE_PF30 >= median_catt1, 1,0) if iCATTLE_PF30 !=. & sample_ols==1
egen median_catt2  = median(iCATTLE_PF30                     ) if                    sample_ols==1 & servicecommand   != 7  
gen median_below2  =   cond(iCATTLE_PF30 <  median_catt2, 1,0) if iCATTLE_PF30 !=. & sample_ols==1 & servicecommand   != 7  
gen median_above2  =   cond(iCATTLE_PF30 >= median_catt2, 1,0) if iCATTLE_PF30 !=. & sample_ols==1 & servicecommand   != 7  

drop median_catt1 median_catt2

xtile aaa_10      = AGRI_PF, n(10), if sample_ols == 1
gen   aaa         = aaa_10  > 1 if aaa_10 != .
xtile frontier1   = tye_tfe890_500k_100_l6, n(2), if sample_ols == 1
xtile frontier2   = tye_tfe890_500k_100_l6, n(2), if sample_ols == 1 & servicecommand   !=   7
recode frontier1 frontier2 (1 = 0) (2 = 1)
xtile  iNDEXP_PCm = iNDEXP_PC, n(2)
recode iNDEXP_PCm (1 = 0) (2 = 1)

ebalance iNDEXP_PCm ww1_vol_sh ww1_awards_pop10_is lpop30 c30unemp c30urban1 ///
	c30farm iYf_T29 MEAN9628 c30men c30black c30jap c30deu c30ita c30vet	 ///
	lc40wage iwarconpro_PC, gen(wiNDEXP_PC)
	
xtile  nd_med = iNDEXP_PC, n(2)
gen nd_m = cond(nd_med == 2,1,0) if nd_med != .
cem ww1_vol_sh (0 .2875817 1) c30unemp (0 .0505954 1) c30urban MEAN9628		 ///
	(0 45 55 100) stateicpsr (#0) if nd_med != ., treatment(nd_m)
for VAR in var cem_strata cem_matched cem_weights : ren VAR nd_VAR

********************************************************************************
****  	j. Keep what is needed and label variables           			    ****
********************************************************************************
keeporder stateicpsr countynd state countyname _ID_WGS84 _ID_CA lon_nd lat_nd lonx_nd latx_nd servicecommand sample_ols CLIMDIVX DISTRICT_ID62 DISTRICT_ID73      			          ///
closestbor_n  dist2stateborder bor_n warbond_PC	iwarbond_1944_PC	iwarconpro_PC																			      					  ///
ww2_vol_pop40 ww2_volw_pop40 ww2_awards_pop40 ww2_awards_pop40_is ww2_volw_pop40 pc1 NDEXP_PC iNDEXP_PC 	      										                              /// 
iSSA_PB iHOLC_PH EW_AL iinfra_AL iRFC_PC SUM3MO_DROUGHT1897 SUM3MO_DROUGHT1898 SUM3MO_DROUGHT1899 SUM3MO_DROUGHT1900 SUM3MO_DROUGHT1901 SUM3MO_DROUGHT1902        		              ///
SUM3MO_DROUGHT1903 SUM3MO_DROUGHT1904 SUM3MO_DROUGHT1905 SUM3MO_DROUGHT1906 SUM3MO_DROUGHT1907 SUM3MO_DROUGHT1908 SUM3MO_DROUGHT1909 SUM3MO_DROUGHT1910 SUM3MO_DROUGHT1911 			  ///
SUM3MO_DROUGHT1912 SUM3MO_DROUGHT1913 SUM3MO_DROUGHT1914 SUM3MO_DROUGHT1915 SUM3MO_DROUGHT1916 SUM3MO_DROUGHT1917 SUM3MO_DROUGHT1918 SUM3MO_DROUGHT1919 SUM3MO_DROUGHT1920 			  ///
SUM3MO_DROUGHT1921 SUM3MO_DROUGHT1922 SUM3MO_DROUGHT1923 SUM3MO_DROUGHT1924 SUM3MO_DROUGHT1925 SUM3MO_DROUGHT1926 SUM3MO_DROUGHT1927 SUM3MO_DROUGHT1928 SUM3MO_DROUGHT1929 			  ///
SUM3MO_DROUGHT1930 SUM3MO_DROUGHT1931 SUM3MO_DROUGHT1932 SUM3MO_DROUGHT1933 SUM3MO_DROUGHT1934 SUM3MO_DROUGHT1935 SUM3MO_DROUGHT1936 SUM3MO_DROUGHT1937 SUM3MO_DROUGHT1938 		      ///
SUM3MO_DROUGHT1939 SUM3MO_DROUGHT1940 iSUM3MO_DROUGHT9700 iSUM3MO_DROUGHT0104 iSUM3MO_DROUGHT0508 iSUM3MO_DROUGHT0912 iSUM3MO_DROUGHT1316 iSUM3MO_DROUGHT1720 	                      ///
iSUM3MO_DROUGHT2124 iSUM3MO_DROUGHT2528 iSUM3MO_DROUGHT2932 SUM3MO_DROUGHT3340 iSUM3MO_DROUGHT3340 iSUM3MO_DROUGHT9732 AGRI_PF iAGRI_PF iAGRI_T62 		          		              ///
AGRICULTURE_T73 iAGRI_T73 ww1_vol_sh ww1_vol_pop10 ww1_awards_pop10_is lpop10 c10unemp c10urban1 c10men c10black c10jap c10deu c10ita c10aut MEAN9628 FDR40 iYf_T29                   ///
lpop30 c30urban1 c30farm c30men c30black c30jap c30deu c30ita c30vet invpop_w99 tye_tfe890_500k_100_l6 shown_1930 dlpop c30unemp c40emp c40unemp c40wage lc40wage                             ///
aaa median_below1 median_above1 median_below2 median_above2 lowsharec lowdpop dust_bowl c40w_10 c40e_10 fdr40_10             

lab var _ID_WGS84           "Geographic identifier (rename it to _ID and use with C-CountiesND_WGS84)"
lab var _ID_CA              "Geographic identifier (rename it to _ID and use with C-CountiesND_CA)"
lab var lon_nd              "Longitude of county centroid (WGS84)"
lab var lat_nd              "Latitude of county centroid (WGS84)"
lab var lonx_nd             "Longitude of county centroid - forced to be inside (WGS84)"
lab var latx_nd             "Latitude of county centroid - forced to be inside (WGS84)"
lab var servicecommand      "Service Command (NARA)"
lab var sample_ols          "=1 if observation is in main sample"
lab var iwarbond_1944_PC    "War bond purchases (1944) p.c. (i.h.s. - county data book)"
lab var iwarconpro_PC       "War contracts per capita (i.h.s. - county data book)"
lab var ww2_vol_pop40       "WW2 volunteers per 100 people (NARA and IPUMS)"
lab var ww2_volw_pop40      "WW2 women volunteers per 100 people (NARA and IPUMS)"
lab var ww2_awards_pop40    "WW2 medals per 1000 people (Homes of Heroes, Hall of Valor and IPUMS)"
lab var ww2_awards_pop40_is "WW2 medals per 1000 people (i.h.s.  - Homes of Heroes, Hall of Valor and IPUMS)"
lab var pc1                 "WW2 patriotism 1st principal component (warbonds, volunteers and medals)"
lab var NDEXP_PC            "New Deal grants per 1930 people (Fishback et al. 2003 and IPUMS)"
lab var iNDEXP_PC           "New Deal grants per 1930 people (i.h.s. - Fishback et al. & IPUMS)"
lab var iSSA_PB             "Social security grants per potential beneficiary (i.h.s.-Fishback et al & IPUMS)"
lab var iHOLC_PH            "HOLC loans per home owners (i.h.s. - Fishback et al & IPUMS)"
lab var EW_AL               "Emergency workers per eligible workers (1938 unemp Census)"
lab var iinfra_AL           "Infrastructure grants per workers (i.h.s. - Fishback et al. & 1938 unemp Census)"
lab var iRFC_PC             "RFC loans per capita (i.h.s. - Fishback et al. & IPUMS)"
lab var iSUM3MO_DROUGHT9700 "Number of summer (Jun-Aug) months with PSDI < -3 in 1897-1900 (i.h.s. - NOAA)"
lab var iSUM3MO_DROUGHT0104 "Number of summer (Jun-Aug) months with PSDI < -3 in 1901-1904 (i.h.s. - NOAA)"
lab var iSUM3MO_DROUGHT0508 "Number of summer (Jun-Aug) months with PSDI < -3 in 1905-1908 (i.h.s. - NOAA)"
lab var iSUM3MO_DROUGHT0912 "Number of summer (Jun-Aug) months with PSDI < -3 in 1909-1912 (i.h.s. - NOAA)"
lab var iSUM3MO_DROUGHT1316 "Number of summer (Jun-Aug) months with PSDI < -3 in 1913-1916 (i.h.s. - NOAA)"
lab var iSUM3MO_DROUGHT1720 "Number of summer (Jun-Aug) months with PSDI < -3 in 1917-1920 (i.h.s. - NOAA)"
lab var iSUM3MO_DROUGHT2124 "Number of summer (Jun-Aug) months with PSDI < -3 in 1921-1924 (i.h.s. - NOAA)"
lab var iSUM3MO_DROUGHT2528 "Number of summer (Jun-Aug) months with PSDI < -3 in 1925-1928 (i.h.s. - NOAA)"
lab var iSUM3MO_DROUGHT2932 "Number of summer (Jun-Aug) months with PSDI < -3 in 1929-1932 (i.h.s. - NOAA)"
lab var  SUM3MO_DROUGHT3340 "Number of summer (Jun-Aug) months with PSDI < -3 in 1933-1940 (NOAA)"
lab var iSUM3MO_DROUGHT9732 " Number of summer (Jun-Aug) months with PSDI < -3 in 1897-1932 (i.h.s. - NOAA)"
lab var iSUM3MO_DROUGHT3340 "Number of summer (Jun-Aug) months with PSDI < -3 in 1933-1940 (i.h.s. - NOAA)"
lab var iAGRI_T62           "Tenure in 62nd agri committee (i.h.s. - Canon et al 1998 & ICPSR)"
lab var iAGRI_T73           "Tenure on agri committee: 1933-35 (i.h.s. - Canon et al 1998 & ICPSR)"
lab var AGRI_PF             "Agricultural support (AAA and FCA) per 1930 farmer (Fishback et al & IPUMS)"
lab var iAGRI_PF            "Agricultural support (AAA and FCA) per 1930 farmer (i.h.s. - Fishback et al & IPUMS)"
lab var ww1_vol_sh          "WW1 volunteering rate (Report of Provost)"
lab var ww1_vol_pop10       "WW1 volunteers per 100 people (Report of Provost & IPUMS)"
lab var ww1_awards_pop10_is "WW1 medals per 1000 people (i.h.s.  - Home of Heroes, Hall of Valor & IPUMS)"
lab var lpop10              "log 1910 population (IPUMS)"
lab var iYf_T29             "1929 farm income per acre (ICPSR)"
lab var lpop30              "log 1930 population (IPUMS)"
lab var invpop_w99          "Inverse of 1930 population, winsorized fraction .01 (IPUMS)"
lab var shown_1930          "Share of farmers who own their farm (ICPSR)"
lab var dlpop               "log(1940 population) - log(1930 population)"
lab var c40emp              "Share of employed in 1940 (IPUMS)"
lab var c40wage             "Average wage of employees in 1939 (IPUMS)"
lab var lc40wage            "log of average wage of employees in 1939 (IPUMS)"
lab var aaa                 "=1 if county is in top 90% of agricultural support (Fishback et al & IPUMS)"
lab var median_below1       "=1 if county is below median level of cattle (ICPSR - full sample)"
lab var median_above1       "=1 if county is above median level of cattle (ICPSR - full sample)"
lab var median_below2       "=1 if county is below median level of cattle (ICPSR - sample exclude 7th Service Command)"
lab var median_above2       "=1 if county is above median level of cattle (ICPSR - sample exclude 7th Service Command)"
lab var lowsharec           "=1 if county has few sharecroppers (more than 52% of farmers own the farm: top 2/3 - ICPSR)"
lab var lowdpop             "=1 if county has low change in population (5th to 95th percentile in 1930-40 change in pop - IPUMS)"
lab var dust_bowl           "=1 if Dust Bowl county (Hornbeck)"
lab var c40w_10             "10 quantiles of 1939 employee wage"
lab var c40e_10             "10 quantiles of 1940 employment rate"
lab var fdr40_10            "10 quantiles of 1940 FDR vote share"

********************************************************************************
****  	k. Save patriotism.dta       	                       			    ****
********************************************************************************
save "data/patriotism", replace

********************************************************************************
****  2. patriotism-panel		                       				    	****
****  	a. First period		                       			     	    	****
********************************************************************************
use "data/patriotism", clear

gen time = 1

gen  SUM3MO_DROUGHT   = SUM3MO_DROUGHT1908 + SUM3MO_DROUGHT1909 + SUM3MO_DROUGHT1910 + SUM3MO_DROUGHT1911 + SUM3MO_DROUGHT1912 + SUM3MO_DROUGHT1913 + SUM3MO_DROUGHT1914 + SUM3MO_DROUGHT1915
gen iSUM3MO_DROUGHT   = asinh(SUM3MO_DROUGHT)

keeporder time stateicpsr countynd countyname servicecommand iNDEXP_PC iAGRI_PF iSUM3MO_DROUGHT SUM3MO_DROUGHT iAGRI_T62 lc40wage iwarconpro_PC MEAN9628 ww1_vol_pop10 ww1_awards_pop10_is  lpop10 c10*

ren iAGRI_T62           iAGRI
ren ww1_awards_pop10_is awards_pop_is
ren ww1_vol_pop10 		vol_pop
ren lpop10 				lpop
for VAR in any unemp urban1 men black jap deu ita aut: ren c10VAR VAR

recode iNDEXP_PC iAGRI_PF (* = 0)

save "tmp/panel_t1", replace

********************************************************************************
****  	b. Second period		                       			     	    ****
********************************************************************************
use "data/patriotism", clear

gen time = 2

gen  SUM3MO_DROUGHT   = SUM3MO_DROUGHT3340
gen iSUM3MO_DROUGHT   = asinh(SUM3MO_DROUGHT)

keeporder time stateicpsr countynd countyname servicecommand iNDEXP_PC iAGRI_PF iSUM3MO_DROUGHT SUM3MO_DROUGHT iAGRI_T73 lc40wage iwarconpro_PC MEAN9628 ww2_vol_pop40 ww2_awards_pop40_is  lpop30 c30*

ren iAGRI_T73           iAGRI
ren ww2_awards_pop40_is awards_pop_is
ren ww2_vol_pop40 		vol_pop
ren lpop30 				lpop
for VAR in any unemp urban1 men black jap deu ita vet: ren c30VAR VAR

save "tmp/panel_t2", replace

********************************************************************************
****  	c. Append the two periods                     			     	    ****
********************************************************************************
use "tmp/panel_t1", replace
append using "tmp/panel_t2"

egen panel = group(stateicpsr countynd countyname)

 gen iSUM3MO_DROUGHT_ww1 = cond(time == 1,iSUM3MO_DROUGHT,0) if iSUM3MO_DROUGHT != .
 gen iSUM3MO_DROUGHT_ww2 = cond(time == 2,iSUM3MO_DROUGHT,0) if iSUM3MO_DROUGHT != .
 gen iAGRI_ww1           = cond(time == 1,iAGRI          ,0) if iAGRI           != .
 gen iAGRI_ww2           = cond(time == 2,iAGRI          ,0) if iAGRI           != .


egen iNDEXP_PCx     = max(iNDEXP_PC), by(panel)
 gen iNDEXP_PC_post = cond(time == 1,0,iNDEXP_PCx) if iNDEXP_PCx != .

egen iAGRI_PFx      = max(iAGRI_PF  ), by(panel)
 gen iAGRI_PF_post  = cond(time == 1,0,iAGRI_PFx  ) if iAGRI_PFx   != .
 
for VAR in var MEAN9628 vet lc40wage iwarconpro_PC : gen VAR2 = cond(time == 2,VAR,0) if VAR != .

recode vet2 (. = 0) if time == 1
recode aut  (. = 0) if time == 2

********************************************************************************
****  	d. Keep what is needed and label variables           			    ****
********************************************************************************
keep if servicecommand != 7

keeporder time panel stateicpsr countynd countyname servicecommand iNDEXP_PC iAGRI_PF vol_pop awards_pop_is lpop unemp urban1 men black jap deu ita aut c30farm vet iSUM3MO_DROUGHT_ww1 iSUM3MO_DROUGHT_ww2 iAGRI_ww1 iAGRI_ww2 iNDEXP_PCx iNDEXP_PC_post iAGRI_PFx iAGRI_PF_post MEAN96282 vet2 lc40wage2 iwarconpro_PC2 

lab var time                  "Period indicator: 1 = WW1, 2 = WW2"
lab var panel                 "Panel (county) indicator"
lab var servicecommand        "Service Command (NARA)"
lab var iNDEXP_PC             "New Deal grants per 1930 people (i.h.s. - Fishback et al. & IPUMS)"
lab var iAGRI_PF              "Agricultural support (AAA and FCA) per 1930 farmer (i.h.s. - Fishback et al & IPUMS)"
lab var vol_pop               "Volunteers per 100 people (Report of Provost, NARA and IPUMS)"
lab var awards_pop_is         "Medals per 1000 people (Homes of Heroes, Hall of Valor and IPUMS)"
lab var lpop                  "log population (IPUMS)"
lab var iSUM3MO_DROUGHT_ww1   "Number of summer (Jun-Aug) months with PSDI < -3 in 8 years before war x WW1 (i.h.s. - NOAA)"
lab var iSUM3MO_DROUGHT_ww2   "Number of summer (Jun-Aug) months with PSDI < -3 in 8 years before war x WW2 (NOAA)"
lab var iAGRI_ww1             "Tenure in agri committee before war x WW1 (i.h.s. - Canon et al 1998 & ICPSR)"
lab var iAGRI_ww2             "Tenure in agri committee before war x WW2 (i.h.s. - Canon et al 1998 & ICPSR)"
lab var iNDEXP_PCx            "New Deal grants per 1930 people (i.h.s. - Fishback et al. & IPUMS)"
lab var iNDEXP_PC_post        "New Deal grants per 1930 people x WW2 (i.h.s. - Fishback et al. & IPUMS)"
lab var iAGRI_PFx             "Agricultural support (AAA and FCA) per 1930 farmer (i.h.s. - Fishback et al & IPUMS)"
lab var iAGRI_PF_post         "Agricultural support (AAA and FCA) per 1930 farmer x WW2 (i.h.s. - Fishback et al & IPUMS)"
lab var MEAN96282             "Mean Democratic vote share: 1898-1928 x WW2 (Fishback et al. 2003)"
lab var vet2                  "Share of veterans among men 30 or older in 1930 x WW2 (IPUMS)"
lab var lc40wage2             "log of average wage of employees in 1939 x WW2 (IPUMS)"
lab var iwarconpro_PC2        "War contracts per capita x WW2 (i.h.s. - county data book)"

********************************************************************************
****  	e. Save patriotism.dta       	                       			    ****
********************************************************************************
save "data/patriotism-panel", replace

********************************************************************************
****  3. patriotism-ASN		                       				        	****
****  	a. County-level variables		                       				****
********************************************************************************
use "data/CountiesND_WGS84", clear

ren _ID _ID_WGS84
merge 1:1 stateicpsr countynd using "data/CountiesND_CA"  , nogen keepus(_ID)
ren _ID _ID_CA
order state-_ID_WGS84 _ID_CA

merge 1:1 stateicpsr countynd using "data/new_deal.dta"                     			  , nogen keep(mas mat)    /* Not matched DC & Yellowstone (MT) */
merge 1:1 stateicpsr countynd using "data/committees.dta"    				  			  , nogen keep(mas mat)    /* Not matched DC */
merge 1:1 stateicpsr countynd using "data/FDR40.dta"              		  			  	  , nogen keep(mas mat)    /* Not matched 29 counties from master */
merge 1:1 stateicpsr countynd using "data/C30.dta"                          			  , nogen keep(mas mat)    /* Not matched Yellowstone (MT) */
merge 1:1 stateicpsr countynd using "data/agriC30.dta"                       			  , nogen keep(    mat)    /* Perfect match */

keeporder stateicpsr state countynd  NDEXP AAA A30FARMERS C30POP C30EMP C30UNEMP MEAN9628 AGRICULTURE*

merge 1:m stateicpsr countynd using "data/Bridge-countynd-countyicpsr1940", nogen /* Perfect match */

replace countyicpsr40 =  605 if stateicpsr == 72 & countyicpsr40 ==  610 /* Union, Oregon.  */
replace countyicpsr40 =    5 if stateicpsr == 65 & countyicpsr40 ==  250 /* Carson, Nevada is really Ormsby, Nevada.  */
replace countyicpsr40 =  250 if stateicpsr == 65 & countyicpsr40 ==  270 /* Pershing, Nevada.  */

for VAR in var NDEXP AAA A30FARMERS C30POP C30EMP C30UNEMP MEAN9628 : replace VAR = VAR * weightnd

collapse (sum) NDEXP AAA A30FARMERS C30POP C30EMP C30UNEMP MEAN9628 (max)  AGRICULTURE* , by(state stateicpsr countyicpsr)

merge m:1 state               	   using "rawdata/NARA/ServiceCommands.dta"          			  	  , nogen keep(mas mat)    /* Not matched are HI & AK from using */
merge 1:1 stateicpsr countyicpsr40 using "data/droughts_ASN.dta"                       			      , nogen keep(mas mat)    /* Perfect match */
merge 1:1 stateicpsr countyicpsr40 using "data/volunteers_ww2_ASN.dta"								  , nogen keep(mas mat)    /* Not matched 29 from master: 25 from state 40; 2 from state 49; 1 from 61 and 1 from 64 */
merge 1:1 stateicpsr countyicpsr40 using "data/C40_ASN.dta"								              , nogen keep(mas mat)    /* Not matched are Nevada, county 510 + Hawaii & Alaska */
merge 1:1 stateicpsr countyicpsr40 using "rawdata/WWI_volunteers/volunteers_ww1_ASN.dta"			  , nogen keep(mas mat)   /* Not matched 33 from master (many states). Not matched 26 from using: 20 from Virginia (40). */

********************************************************************************
****  	b. Create county-level variables			                       	****
********************************************************************************

gen iNDEXP_PC           = asinh(NDEXP       / C30POP    )
gen iAAA_PF             = asinh(AAA    	    / A30FARMERS)
gen iSUM3MO_DROUGHT3340 = asinh(SUM3MO_DROUGHT3340)
gen iAGRI_T73           = asinh(AGRICULTURE_T73)
gen ww2_vol_pop40       =      (WW2VOLDA / C40POP    ) * 100
gen ww1_vol_sh          =       WW1VOL       / (WW1VOL + WW1IND)
gen c30unemp  		    = 		C30UNEMP    / (C30EMP      + C30UNEMP  )
gen lpop30              = log(C30POP )
gen lpop40              = log(C40POP )
gen lc40wage            = log(c40wage)

gen sample_ols = 1 if iNDEXP_PC != . & iAAA_PF != . &  iSUM3MO_DROUGHT3340 != . & iAGRI_T73 !=. & ww2_vol_pop40 !=. & ww1_vol_sh !=. & lpop30 != . & lpop40 != . & lc40wage != . & c40unemp != . & c30unemp != .

********************************************************************************
****  	c. Save county-level variables						                ****
********************************************************************************
keep if sample_ols == 1
keeporder stateicpsr countyicpsr servicecommand iAAA_PF iSUM3MO_DROUGHT3340 iAGRI_T73 sample_ols

save "data/patriotism_countyicpsr40", replace

********************************************************************************
****  	d. Create individual dataset		                       		    ****
********************************************************************************

use "data/ASN-NARA", clear
replace countyicpsr = 1210 if stateicpsr == 44 & countyicpsr ==  410 /* Campbell, Georgia already belonged to Fulton in the 1940 shapefile. */
replace countyicpsr = 1210 if stateicpsr == 44 & countyicpsr == 2030 /* Milton, Georgia already belonged to Fulton in the 1940 shapefile. */

merge m:1 stateicpsr countyicpsr40 using "data/patriotism_countyicpsr40", nogen keep(mas mat)

gen iAAA_PF_farmer  					= iAAA_PF * farmer
gen iAAA_PF_farmhand  					= iAAA_PF * farmhand
gen iSUM3MO_DROUGHT3340_farmer			= iSUM3MO_DROUGHT3340 * farmer
gen iSUM3MO_DROUGHT3340_farmhand		= iSUM3MO_DROUGHT3340 * farmhand
gen iAGRI_T73_farmer					= iAGRI_T73		      * farmer
gen iAGRI_T73_farmhand					= iAGRI_T73		      * farmhand

for VAR in var iAAA_PF_farmer iAAA_PF_farmhand iSUM3MO_DROUGHT3340_farmer iSUM3MO_DROUGHT3340_farmhand iAGRI_T73_farmer iAGRI_T73_farmhand: replace VAR = VAR / 1000

gen serial = _n

egen countyn = group(stateicpsr countyicpsr40)

save "tmp/ASN", replace

********************************************************************************
****  	e. Create CEM for farmers and farm-hands                            ****
****  		  i. Prepare database by keeping only variables neeeded         ****
********************************************************************************
use  "tmp/ASN", replace

keep if sample_ols == 1 & servicecommand != 7 & agri == 1

keep serial countyn farmer farmhand h w C40NONWHITE C40MARRIED noncitizen C40SCHOOL_E  C40SCHOOL_H C40SCHOOL_C C40AGE

********************************************************************************
****  		 ii. Create CEM indicators separately for each county           ****
****                 * Loop over every county in the database               ****
********************************************************************************
levelsof countyn, local(counties)

foreach county of local counties {
	preserve
		keep if countyn == `county'

********************************************************************************
****                 * Create CEM indicators                                ****
****                    - Treatment: being a farmer                         ****
********************************************************************************
		sum farmer if farmhand == 0
		if r(min) == 0 & r(max) == 1 {
			cem             C40NONWHITE(#0) C40MARRIED(#0) noncitizen(#0) C40SCHOOL_E(#0) C40SCHOOL_H(#0) C40SCHOOL_C(#0) C40AGE(#0) if farmhand == 0, treatment(farmer)
		    cap drop cem_treat 
		    for VAR in var cem_strata cem_weights cem_matched : ren VAR VAR_farmer
		} /* if farmer both 0 & 1 */

********************************************************************************
****                    - Treatment: being a farm-hand                      ****
********************************************************************************
		sum farmhand if farmer == 0
		if r(min) == 0 & r(max) == 1 {
			cem             C40NONWHITE(#0) C40MARRIED(#0) noncitizen(#0) C40SCHOOL_E(#0) C40SCHOOL_H(#0) C40SCHOOL_C(#0) C40AGE(#0) if farmer  == 0, treatment(farmhand)
		    cap drop cem_treat 
		    for VAR in var cem_strata cem_weights cem_matched : ren VAR VAR_farmhand
		} /* if farmhand both 0 & 1 */

********************************************************************************
****                 * Save CEM indicators for this county and drop it      ****
********************************************************************************
		drop countyn farmer farmhand h w C40NONWHITE C40MARRIED noncitizen C40SCHOOL_E  C40SCHOOL_H C40SCHOOL_C 
		compress
		save "tmp/cem_`county'", replace
	restore
	drop if countyn == `county'
} /* foreach county */

********************************************************************************
****  		iii. Append CEM indicators for every county and save            ****
********************************************************************************
clear
foreach county of local counties {
	append using "tmp/cem_`county'"
} /* foreach county */

compress cem_*

save "tmp/ASN_cem", replace

********************************************************************************
****  	f. Merge everything, keep only what is needed and label variables   ****
********************************************************************************
use  "tmp/ASN", replace

merge 1:1 serial using "tmp/ASN_cem", nogen

gen insample = sample_ols == 1 & servicecommand != 7 & agri == 1
for VAR in var h w bmi C40NONWHITE C40MARRIED noncitizen C40SCHOOL_E  C40SCHOOL_H C40SCHOOL_C : replace insample = 0 if insample == 1 & VAR == .

lab var iAAA_PF_farmer  			 "AAA grants (i.h.s) $\times$ farmer"
lab var iAAA_PF_farmhand  			 "AAA grants (i.h.s) $\times$ farmhand"
lab var iSUM3MO_DROUGHT3340_farmer	 "Months of drought (i.h.s.): 1933-40 $\times$ farmer"
lab var iSUM3MO_DROUGHT3340_farmhand "Months of drought (i.h.s.): 1933-40 $\times$ farm-hand"
lab var iAGRI_T73_farmer			 "Tenure on agri committee (i.h.s.): 1933-35 $\times$ farmer"
lab var iAGRI_T73_farmhand			 "Tenure on agri committee (i.h.s.) $\times$ farm-hand"
lab var h                            "Height"
lab var w                            "Weight"
lab var bmi                          "BMI"
lab var C40NONWHITE                  "Non white"
lab var C40MARRIED                   "Ever married"
lab var noncitizen                   "Non citizen"
lab var C40SCHOOL_E                  "Elementary school"
lab var C40SCHOOL_H                  "High school"
lab var C40SCHOOL_C                  "College"

foreach treat in farmer farmhand {
	if "`treat'" == "farmer"   local lab "being a farmer"
	if "`treat'" == "farmhand" local lab "being a farm-hand"
	lab var cem_matched_`treat'      "Matched by CEM: treatment is `lab'"
} /* foreach treat */

keep if insample == 1

keeporder stateicpsr countyicpsr countyn volunteer farmer farmhand iAAA_PF_farmer iAAA_PF_farmhand iSUM3MO_DROUGHT3340_farmer iSUM3MO_DROUGHT3340_farmhand iAGRI_T73_farmer iAGRI_T73_farmhand ///
	      h w bmi C40NONWHITE C40MARRIED noncitizen C40SCHOOL_E  C40SCHOOL_H C40SCHOOL_C C40AGE cem_matched_farmhand cem_matched_farmer

********************************************************************************
****  	g. Save patriotism-asn.dta       	                       			****
********************************************************************************
save "data/patriotism-asn", replace

********************************************************************************
****  4. Erase junk				                                            ****
********************************************************************************
erase "tmp/panel_t1.dta"
erase "tmp/panel_t2.dta"
erase "tmp/ASN.dta"
erase "tmp/ASN_cem.dta"

foreach county of local counties {
	erase "tmp/cem_`county'.dta"
} /* foreach county */


rmdir "tmp/"

log close
exit